/*
 * D.A.O. for JPA  
 * Created on 6 juin 2013 ( Time 15:03:36 )
 */

package org.demo.plagehoraire.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.telosys.starterkits.strutsjpa.CoreUtils;
import org.telosys.starterkits.strutsjpa.GenericJpaDao;

import org.demo.plagehoraire.bean.Plagehoraire;

public class JpaPlagehoraireDAO extends GenericJpaDao<Plagehoraire, Integer> {

	private static final Predicate[] ap = {};
	
	/**
	 * Constructs a DAO for Plagehoraire
	 */
	public JpaPlagehoraireDAO() {
		super(Plagehoraire.class);
	}
	
	/**
	 * Search by Plagehoraire example
	 */
	public List<Plagehoraire> search(final Plagehoraire element) {
		final EntityManager em = getEntityManager();
		if ( element != null  ) {
			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Plagehoraire> criteriaQuery = criteriaBuilder.createQuery(Plagehoraire.class);
			Root<Plagehoraire> from = criteriaQuery.from(Plagehoraire.class);

			List<Predicate> listP = new ArrayList<Predicate>();


			if (CoreUtils.isNotBlank(element.getIdplagehoraire())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idplagehoraire"), element.getIdplagehoraire());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdcalendrier())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idcalendrier"), element.getIdcalendrier());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getJoursemaine())) {
				Predicate predicate = criteriaBuilder.equal(from.get("joursemaine"), element.getJoursemaine());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getHentree())) {
				Predicate predicate = criteriaBuilder.equal(from.get("hentree"), element.getHentree());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getMentree())) {
				Predicate predicate = criteriaBuilder.equal(from.get("mentree"), element.getMentree());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getHsortie())) {
				Predicate predicate = criteriaBuilder.equal(from.get("hsortie"), element.getHsortie());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getMsortie())) {
				Predicate predicate = criteriaBuilder.equal(from.get("msortie"), element.getMsortie());
				listP.add(predicate);
			}
			
			criteriaQuery.where(listP.toArray(ap));
			TypedQuery<Plagehoraire> tq = em.createQuery(criteriaQuery);
			return tq.getResultList();
		}
		else
		{
    		return this.loadAll();
		}
	}

}